﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Praktyka.Models;
using Windows.Storage.Streams;
using Windows.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;



// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Praktyka
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage
    {
        private PictureManager picManager { get; set; }

        GestureRecognizer gr = new GestureRecognizer();
        public MainPage()
        {
            InitializeComponent();
            #region gestureSettings
            PointerPressed += MainPage_PointerPressed;
            PointerMoved += MainPage_PointerMoved;
            PointerReleased += MainPage_PointerReleased;
            gr.CrossSliding += gr_CrossSliding;
            gr.Dragging += gr_Dragging;
            gr.Holding += gr_Holding;
            gr.ManipulationCompleted += gr_ManipulationCompleted;
            gr.ManipulationInertiaStarting += gr_ManipulationInertiaStarting;
            gr.ManipulationStarted += gr_ManipulationStarted;
            gr.ManipulationUpdated += gr_ManipulationUpdated;

            gr.RightTapped += gr_RightTapped;
            gr.Tapped += gr_Tapped;
            gr.GestureSettings =
                GestureSettings.ManipulationRotate |
                GestureSettings.ManipulationTranslateX |
                GestureSettings.ManipulationTranslateY |
                GestureSettings.ManipulationScale |
                GestureSettings.ManipulationRotateInertia |
                GestureSettings.ManipulationScaleInertia |
                GestureSettings.ManipulationTranslateInertia |
                GestureSettings.Tap;


            #endregion

            #region fillImages
            picManager = new PictureManager();
            // Task<IEnumerable<string>> fileList = picManager.GetImagesFromFolder("dataImages");
            // picManager.LoadImages(fileList);
            #endregion
        }

        private void gr_Dragging(GestureRecognizer sender, DraggingEventArgs args)
        {
            Debug.WriteLine(string.Format("Dragging :{0},{1}", args.Position.X, args.Position.Y));
            //throw new NotImplementedException();

        }

        private void gr_Holding(GestureRecognizer sender, HoldingEventArgs args)
        {
            //   throw new NotImplementedException();
        }

        private void gr_ManipulationCompleted(GestureRecognizer sender, ManipulationCompletedEventArgs args)
        {
            var info = string.Format("[Manipulation Completed] x:{0} y:{1}", args.Position.X, args.Position.Y);
            Debug.WriteLine(info);
            // throw new NotImplementedException();
        }

        private void gr_ManipulationInertiaStarting(GestureRecognizer sender, ManipulationInertiaStartingEventArgs args)
        {
            var cumulativeTranslation = string.Format("cum Translation={0}", args.Cumulative.Translation);
            var translation = string.Format("translation={0}", args.Delta.Translation);
            var velocitiesLinear = string.Format("vel Linear={0}", args.Velocities.Linear);
            var position = string.Format("position x={0} y={1}", args.Position.X, args.Position.Y);
            var info = string.Format("[gr_ManipulationInertiaStarting]");
            Debug.WriteLine(info);
            Debug.WriteLine(cumulativeTranslation);
            Debug.WriteLine(translation);
            Debug.WriteLine(velocitiesLinear);
            Debug.WriteLine(position);
            Debug.WriteLine("/" + info);

            //throw new NotImplementedException();
        }

        private void gr_ManipulationStarted(GestureRecognizer sender, ManipulationStartedEventArgs args)
        {
            var info = string.Format("[gr_ManipulationStarted] x:{0} y:{1}", args.Position.X, args.Position.Y);
            Debug.WriteLine(info);
            // throw new NotImplementedException();

        }

        private void gr_ManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs args)
        {
            //var info = string.Format("[gr_ManipulationUpdated] x:{0} y:{1}", args.Position.X, args.Position.Y);
            //Debug.WriteLine(info);
            //  throw new NotImplementedException();

        }

        private void gr_RightTapped(GestureRecognizer sender, RightTappedEventArgs args)
        {
            Debug.WriteLine("gr_RightTapped");
            // throw new NotImplementedException();
        }

        private void gr_Tapped(GestureRecognizer sender, TappedEventArgs args)
        {
            Debug.WriteLine("gr_Tapped");
            // throw new NotImplementedException();
        }

        private void gr_CrossSliding(GestureRecognizer sender, CrossSlidingEventArgs args)
        {
            Debug.WriteLine("gr_CrossSliding");
            //  throw new NotImplementedException();
        }

        private void MainPage_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            Debug.WriteLine("Pointer Released");
            var ps = e.GetIntermediatePoints(null);
            if(ps != null && ps.Count > 0)
            {
                gr.ProcessUpEvent(ps[0]);
                e.Handled = true;
                gr.CompleteGesture();
            }
        }

        private void MainPage_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            //Debug.WriteLine("Pointer Moved");
            gr.ProcessMoveEvents(e.GetIntermediatePoints(null));
            e.Handled = true;
        }

        private void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Debug.WriteLine("Pointer Pressed");
            var ps = e.GetIntermediatePoints(null);
            if(ps != null && ps.Count > 0)
            {
                gr.ProcessDownEvent(ps[0]);
                e.Handled = true;
            }
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }


        private async void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            var pic = new PictureManager();
            List<string> images = await pic.GetImages();
            bool hasValue = images.Count > 0;
            Debug.WriteLine(string.Format("Has value {0}", hasValue));
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if(picManager.HasNext())
            {
                imgImageMain.Source = picManager.Next();
            }
        }
    }
}
